其他
代码篇 || 基于Matlab、EEGLab和ERPlab的偏侧化差异波成分分析
%% --------------------------- Set Paths --------------------------------
% Where to load the raw data from .CNT file
rawdatapath = pwd;
% Where to save to
datapath = pwd;
% Current directory
cd(pwd);
% Add to path and open EEGLAB
eeglab;
%% --------------------------- Initializing -----------------------------
%'...= 0' means do not perform step, '...= 1' means perform step
IMPORT = 0; %Imports the eeg recodings and re-reference them to the mastoids
FILTER = 0; %Filters the data
SEGMENT = 0; %Epochs the data
ARTIFACT = 0; %Removes blink and eye movement trials
AVERAGE = 0; %Averaged ERPs for each condition
SPLIT = 0; %Lateralized ERPs
%% --------------------------- Participant IDs --------------------------
subjs = {'111','1112','1113','1114','1115','1116','1118','1119','1120','1121','1123'...
'1124','1125','1126','1130','1131','1132','1133','1134','1135','1136'...
'1138','1139','1141','1142','1143','1145','1146','1148','1149','1150'...
'1151','1152','1153','1154','1155','1156','1157','1158','1160','1161'...
'1163','1164','1165','1166','1167','1169','1170'};
%% ----------------------- Import and Re-Reference ----------------------
if IMPORT == 1
for i = 1:length(subjs) %Loop through participants
s = num2str(subjs{i});
subjdatapath = strcat (datapath, s);subjdatapath = strcat (subjdatapath, '/');
fprintf('********* importing %s \n', subjs{i});
EEG = pop_loadcnt([rawdatapath, subjs{i} '.CNT'] , 'dataformat', 'auto', 'memmapfile', '');
EEG.setname=[subjs{i} '_raw'];
EEG = eeg_checkset( EEG );
EEG = pop_reref( EEG, [65 66] ); %Re-reference to mastoids
EEG.setname=[subjs{i} '_rr'];
EEG = pop_saveset( EEG, 'filename', [subjs{i} '_rr.set'],'filepath', subjdatapath);
EEG = eeg_checkset( EEG );
EEG = pop_delset(EEG, [1]);
end
end
%% --------------------------- Filtering --------------------------------
if FILTER == 1 %Highpass 0.1, lowpass 40 (butterworth bandpass filter)
for i = 1:length(subjs) %lLoop through participants
s = num2str(subjs{i});
subjdatapath = strcat (datapath, s);subjdatapath = strcat (subjdatapath, '/');
fprintf('********* filtering subject %s \n', subjs{i});
EEG = pop_loadset('filename',[subjs{i} '_rr.set'],'filepath',subjdatapath);
EEG = eeg_checkset( EEG );
EEG = pop_basicfilter( EEG, 1:69 , 'Boundary', 'boundary', 'Cutoff', [0.1 40], 'Design', 'butter', 'Filter', 'bandpass', 'Order', 2, 'RemoveDC', 'on' );
EEG.setname='_rr_filt';
EEG = eeg_checkset( EEG );
EEG = pop_saveset( EEG, 'filename', [subjs{i} '_rr_filt.set'],'filepath',subjdatapath);
EEG = eeg_checkset( EEG );
EEG = pop_delset(EEG, [1]);
end
end
%% -------------------- Event list, bin, and epoch ----------------------
if SEGMENT == 1 %Create ERP segements
fprintf('******** segmenting \n');
for i = 1:length(subjs) %Loop through participants
s = num2str(subjs{i});
subjdatapath = strcat (datapath, s);subjdatapath = strcat (subjdatapath, '/');
EEG = pop_loadset('filename',[subjs{i} '_rr_filt.set'],'filepath',subjdatapath);
EEG = eeg_checkset( EEG );
EEG = pop_creabasiceventlist( EEG , 'AlphanumericCleaning', 'on', 'BoundaryNumeric', { -99 }, 'BoundaryString', { 'boundary' }, 'Eventlist', [datapath 'temp.txt']);
EEG = eeg_checkset( EEG );
EEG = pop_binlister( EEG , 'BDF', [datapath 'manyCircles_bins_accuracy.txt'], 'ExportEL', [subjdatapath subjs{i} '_EL.txt'], 'IndexEL', 1, 'SendEL2', 'EEG&Text', 'Voutput', 'EEG' );
EEG = eeg_checkset( EEG );
EEG = pop_epochbin( EEG , [-200.0 600.0], [-200, 0]); %Epoch from -200 to 600 ms, baseline from -200 to 0 ms search array onset
EEG.setname=[subjs{i} '_rr_filt_bin'];
EEG = eeg_checkset( EEG );
EEG = pop_saveset( EEG, 'filename', [subjs{i} '_rr_filt_binaccuracy.set'],'filepath', subjdatapath);
EEG = eeg_checkset( EEG );
EEG = pop_delset(EEG, [1]);
end
end
%% ------------------------ Artifact Detection --------------------------
if ARTIFACT == 1 %Remove blinks, lateral eye movements, and high voltage trials
fprintf('******* artifacting \n');
for i = 1:length(subjs) %Loop through participants
s = num2str(subjs{i});
subjdatapath = strcat (datapath, s);subjdatapath = strcat (subjdatapath, '/');
EEG = pop_loadset('filename',[subjs{i} '_rr_filt_binaccuracy.set'],'filepath',subjdatapath);
EEG = eeg_checkset( EEG );
%adding EOG channels
EEG = pop_eegchanoperator( EEG, [datapath 'EOG_chan_list_CNT.txt'] , 'ErrorMsg', 'popup', 'Warning', 'off' );
EEG = eeg_checkset( EEG );
%blinks 80 mv from -200 to 400 ms
EEG = pop_artmwppth( EEG , 'Channel', [71 72], 'Flag', 1, 'Threshold', 80, 'Twindow', [-200 400], 'Windowsize', 200, 'Windowstep', 100 );
EEG = eeg_checkset( EEG );
%eye movements 32 mv from -200 to 400 ms
EEG = pop_artstep( EEG , 'Channel', 70, 'Flag', 2, 'Threshold', 32, 'Twindow', [-200 400], 'Windowsize', 200, 'Windowstep', 50 );
EEG = eeg_checkset( EEG );
%channel activity greater or less than 100 uV in posterior channels
%from -200 to 400 ms
EEG = pop_eegthresh(EEG, 1, [20:27 57:64], -100, 100, -0.2, 0.4, 1, 0);
EEG = pop_saveset( EEG, 'filename',[subjs{i} '_rr_filt_bin_artaccuracy.set'],'filepath',subjdatapath);
EEG = eeg_checkset( EEG );
EEG = pop_delset(EEG, [1]);
end
end
%% ---------------------------- Averaging -------------------------------
if AVERAGE == 1 %Average across channels
fprintf('********* averaging \n');
for i = 1:length(subjs) %Loop through participants
s = num2str(subjs{i});
subjdatapath = strcat (datapath, s);subjdatapath = strcat (subjdatapath, '/');
EEG = pop_loadset('filename',[subjs{i} '_rr_filt_bin_artaccuracy.set'],'filepath',subjdatapath);
EEG = eeg_checkset( EEG );
ERP = pop_averager( EEG , 'Criterion', 'good', 'DSindex', 1, 'ExcludeBoundary', 'on', 'SEM', 'on' );
ERP = pop_savemyerp(ERP, 'erpname',...
[subjs{i} '_ERP'], 'filename', [subjs{i} '_ERPaccuracy.erp'], 'filepath', subjdatapath, 'Warning', 'off');
EEG = pop_delset(EEG, [1]);
end
end
%% ---------------------- Difference Waveforms --------------------------
if SPLIT == 1 %Split by left/right trials
fprintf('******** riding camels \n');
for i = 1:length(subjs) %Loop through participants
s = num2str(subjs{i});
subjdatapath = strcat (datapath, s);subjdatapath = strcat (subjdatapath, '/');
ERP = pop_loaderp('filename',[subjs{i} '_ERPaccuracy.erp'],'filepath',subjdatapath);
ERP = eeg_checkset( ERP );
ERP = pop_binoperator( ERP, { 'prepareContraIpsi', 'Lch = [ 20:27 65 71]', 'Rch = [ 57:64 66 72]', 'nbin1 = 0.5*bin3@Rch + 0.5*bin1@Lch label 100TargetCorrect Contra',...
'nbin2 = 0.5*bin3@Lch + 0.5*bin1@Rch label 100TargetCorrect Ipsi', 'nbin3 = 0.5*bin4@Rch + 0.5*bin2@Lch label 100TargetIncorrect Contra',...
'nbin4 = 0.5*bin4@Lch + 0.5*bin2@Rch label 100TargetIncorrect Ipsi', 'nbin5 = 0.5*bin7@Rch + 0.5*bin5@Lch label 100DistractorCorrect Contra',...
'nbin6 = 0.5*bin7@Lch + 0.5*bin5@Rch label 100DistractorCorrect Ipsi',...
'nbin7 = 0.5*bin8@Rch + 0.5*bin6@Lch label 100DistractorIncorrect Contra', 'nbin8 = 0.5*bin8@Lch + 0.5*bin6@Rch label 100DistractorIncorrect Ipsi',...
'nbin9 = 0.5*bin11@Rch + 0.5*bin9@Lch label 0TargetCorrect Contra', 'nbin10 = 0.5*bin11@Lch + 0.5*bin9@Rch label 0TargetCorrect Ipsi',...
'nbin11 = 0.5*bin12@Rch + 0.5*bin10@Lch label 0TargetIncorrect Contra', 'nbin12 = 0.5*bin12@Lch + 0.5*bin10@Rch label 0TargetIncorrect Ipsi',...
'nbin13 = 0.5*bin15@Rch + 0.5*bin13@Lch label 0DistractorCorrect Contra', 'nbin14 = 0.5*bin15@Lch + 0.5*bin13@Rch label 0DistractorCorrect Ipsi',...
'nbin15 = 0.5*bin16@Rch + 0.5*bin14@Lch label 0DistractorIncorrect Contra', 'nbin16 = 0.5*bin16@Lch + 0.5*bin14@Rch label 0DistractorIncorrect Ipsi',...
'nbin17 = 0.5*bin19@Rch + 0.5*bin17@Lch label 50TargetCorrect Contra', 'nbin18 = 0.5*bin19@Lch + 0.5*bin17@Rch label 50TargetCorrect Ipsi',...
'nbin19 = 0.5*bin20@Rch + 0.5*bin18@Lch label 50TargetIncorrect Contra', 'nbin20 = 0.5*bin20@Lch + 0.5*bin18@Rch label 50TargetIncorrect Ipsi',...
'nbin21 = 0.5*bin23@Rch + 0.5*bin21@Lch label 50DistractorCorrect Contra', 'nbin22 = 0.5*bin23@Lch + 0.5*bin21@Rch label 50DistractorCorrect Ipsi',...
'nbin23 = 0.5*bin24@Rch + 0.5*bin22@Lch label 50DistractorIncorrect Contra', 'nbin24 = 0.5*bin24@Lch + 0.5*bin22@Rch label 50DistractorIncorrect Ipsi'});
ERP = pop_binoperator( ERP,{'bin25 = bin1 - bin2 label 100TargetC Contra-Ipsi',...
'bin26 = bin3 - bin4 label 100TargetI Contra-Ipsi', 'bin27 = bin5 - bin6 label 100DistractorC Contra-Ipsi', 'bin28 = bin7 - bin8 label 100DistractorI Contra-Ipsi',...
'bin29 = bin9 - bin10 label 0TargetC Contra-Ipsi', 'bin30 = bin11 - bin12 label 0TargetI Contra-Ipsi',...
'bin31 = bin13 - bin14 label 0DistractorC Contra-Ipsi', 'bin32 = bin15 - bin16 label 0DistractorI Contra-Ipsi', 'bin33 = bin17 - bin18 label 50TargetC Contra-Ipsi',...
'bin34 = bin19 - bin20 label 50TargetI Contra-Ipsi', 'bin35 = bin21 - bin22 label 50DistractorC Contra-Ipsi',...
'bin36 = bin23 - bin24 label 50DistractorI Contra-Ipsi'});
ERP = pop_savemyerp(ERP, 'erpname', [subjs{i} '_ERP_splitaccuracy'], 'filename', [subjs{i} '_ERP_splitaccuracy.erp'], 'filepath', subjdatapath);
end
end
因个人精力有限,难免有不足之处。如有不当或遗漏,欢迎大家留言交流。谢谢!
完